// pages/answer/answer.js
const api = require("../../api").API;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    current: 0,
    dailyProblemList: [],
  },
  toSelect(e) {
    // console.log(e.currentTarget.dataset.index);
    let index = e.currentTarget.dataset.index
    let optionid = e.currentTarget.dataset.optionid
    this.data.dailyProblemList[index].selected = optionid;
    this.setData({
      dailyProblemList: this.data.dailyProblemList
    })
    // console.log(this.data.dailyProblemList);
  },
  // 点击下一题按钮
  toNext() {
    // console.log(this.data.dailyProblemList[this.data.current].selected);
    if (this.data.current >= this.data.dailyProblemList.length - 1) {
      return
    }
    if (!this.data.dailyProblemList[this.data.current].selected) {
      return wx.showToast({
        title: '请选择选项！',
        icon: 'none'
      })
    }

    this.setData({
      current: this.data.current + 1
    })
  },
  // 点击上一题按钮
  toBefore() {
    if (this.data.current <= 0) {
      return
    }
    this.setData({
      current: this.data.current - 1
    })
  },
  // 点击提交答案按钮
  confirmAnswer() {
    if (!this.data.dailyProblemList[this.data.current].selected) {
      return wx.showToast({
        title: '请选择选项！',
        icon: 'none'
      })
    }
    let submitPrbList = [];
    for (let i of this.data.dailyProblemList) {
      let item = {};
      item.topicId = i.topicId;
      item.optionId = i.selected;
      submitPrbList.push(item);
    }
    let data = {
      userId: wx.getStorageSync('userId'),
      topicType: "1",
      submitPrbList: submitPrbList
    }
    api.prbSubmit(data).then(res => {
      if (res.code === '200') {
        wx.showModal({
          title: '提示',
          showCancel: false,
          content: `正确：${res.data.successCount}，错误：${res.data.errorCount}`,
          complete: (res) => {
            if (res.confirm) {
              wx.switchTab({
                url: '/pages/index/index',
              })
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    api.getDaily({}).then(res => {
      if (res.code === '200') {
        this.setData({
          dailyProblemList: res.data.dailyProblemList
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})